﻿using LiveShopvnext.Helper;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Snowflake.Core;
using Microsoft.EntityFrameworkCore;
using Volo.Abp.Application.Dtos;
using Microsoft.AspNetCore.Authorization; //[Authorize] //[AbpAuthorize("")]
using Microsoft.Extensions.Caching.Distributed;

namespace LiveShopvnext.Order
{
    //[Authorize] adrs
    //[AbpAuthorize("")]
    public class OrderAppService : ApplicationService, IOrderAppService
    {
        public IDistributedCache redisHelper { get; set; }

        private readonly IRepository<Order.OrderInfo, int> _orderInfo;
        private readonly IRepository<Order.OrderAttached, int> _orderAttached;
        private readonly IRepository<Commodity.Product, int> _product;

        public OrderAppService(
                IRepository<Order.OrderInfo, int> orderInfos,
                IRepository<Order.OrderAttached, int> orderAttacheds,
                IRepository<Commodity.Product, int> product
            )
        {
            _orderInfo = orderInfos;
            _orderAttached = orderAttacheds;
            _product= product;
        }

        #region  订单添加
        /// <summary>
        /// 订单添加
        /// </summary>
        /// <param name="infoDto"></param>
        /// <returns></returns>
        /// 
        [Authorize]
        [HttpPost]
        public string OrderCreate(CreateUpdateOrferInfoDto infoDto)
        {
            List<CreateUpdateOrderAttached> attached = infoDto.CreateUpdateOrderAttached;
            // 生成雪花id
            string Snowflake = new IdWorker(1, 1).NextId().ToString();

            var orderfos = new Order.OrderInfo
            {
                orderInfoId = Snowflake,
                payWayInfoId = infoDto.payWayInfoId,
                usersId = infoDto.usersId,
                orderInfoAddre = infoDto.orderInfoAddre,
                orderInfoName = infoDto.orderInfoName,
                shoppAddreTel = infoDto.shoppAddreTel,
                orderInfoSalePrice = infoDto.orderInfoSalePrice,
                commodCountPrice = infoDto.orderInfoSalePrice,
                orderInfoBarCode = infoDto.orderInfoBarCode,
                orderInfoPayPrice = infoDto.orderInfoPayPrice,
                orderInfoActivePrice = infoDto.orderInfoActivePrice,
                orderInfoWaybillNum = Snowflake
            };

            List<OrderAttached> orderAttached = new List<OrderAttached>();

            foreach (var item in attached)
            {
                orderAttached.Add(new OrderAttached
                {
                    orderInfoId = Snowflake,
                    productId = item.productId,
                    productSum = item.productSum,
                    CountPrice = item.countPrice
                });
            }

            _orderInfo.InsertAsync(orderfos);
            _orderAttached.InsertManyAsync(orderAttached);

            return "下单成功";
        }
        #endregion

        #region 订单显示
        /// <summary>
        /// 订单显示
        /// </summary>
        /// <param name="orderInfoListDto"></param>
        /// <returns></returns>
        public PagedResultDto<OrderInfoDto> GetOrderInfoListDto(OrderInfoListDto orderInfoListDto)
       {
            var infoDtos = (
                    from a in _orderInfo
                    orderby a.orderInfoTime descending
                    select new OrderInfoDto
                    {
                        Id=a.Id,
                        orderInfoTime = a.orderInfoTime,
                        payWayInfoId = a.payWayInfoId,
                        orderInfoAddre = a.orderInfoAddre,
                        orderInfoName = a.orderInfoName,
                        shoppAddreTel = a.shoppAddreTel,
                        orderInfoSalePrice = a.orderInfoSalePrice,
                        orderInfoBarCode = a.orderInfoBarCode,
                        orderInfoPayPrice = a.orderInfoSalePrice,
                        orderInfoState = a.orderInfoState,
                        orderInfoWaybillNum = a.orderInfoWaybillNum,
                        GetOrderAttachedDto = (
                                     from b in _orderAttached
                                     join c in _product
                                     on b.productId equals c.Id
                                     where b.orderInfoId == a.orderInfoId
                                     select new OrderAttachedDto
                                     {
                                         productId=c.Id,
                                         productName = c.productName,
                                         productSPromotion = c.productSPromotion,
                                         productWeight = c.productWeight,
                                         productSum = b.productSum,
                                         CountPrice = b.CountPrice
                                     }
                                 ).ToList()
                    }
                ).AsSingleQuery<OrderInfoDto>().ToList();

            var list = infoDtos
                .WhereIf(orderInfoListDto.orderInfoState >= 0, c => c.orderInfoState == orderInfoListDto.orderInfoState)
                .WhereIf(!string.IsNullOrEmpty(orderInfoListDto.orderInfoWaybillNum),c=>c.orderInfoWaybillNum.Contains(orderInfoListDto.orderInfoWaybillNum))
                .ToList();

            var totalCount = list.Count();

            //分页
            list = list
                .Skip(orderInfoListDto.SkipCount)
                .Take(orderInfoListDto.MaxResultCount).ToList();

            return new PagedResultDto<OrderInfoDto>(totalCount, list);
        }
        #endregion

        #region  取消订单
        /// <summary>
        /// 取消订单
        /// </summary>
        /// <param name="orderInfoWaybillNum"></param>
        /// <returns></returns>
        [HttpDelete]
        public int OrderDelete(string orderInfoWaybillNum)
        {
            //int[] orderAttached, orderInfo;

            // orderInfo = _orderInfo.Where(x => x.orderInfoWaybillNum == orderInfoWaybillNum)
            //    .Select(x => x.Id).ToArray();  

            //if (orderInfo.Length>0)
            //{
            //    orderAttached = _orderAttached.Where(x => x.orderInfoId == orderInfoWaybillNum)
            //        .Select(x=>x.Id).ToArray();
            //}
            //else
            //{
            //    return 0;
            //}
            //_orderInfo.DeleteManyAsync(orderInfo);
            //_orderAttached.DeleteManyAsync(orderAttached);

            return 1;
        }

        #endregion
    }
}